<?php
class ActionModel {
	private $_aStatus = array ('active' => '启用', 'close' => '禁用' );
	
	public function getList() {
		$sSql = 'SELECT * FROM actions ORDER BY act_name';
		$aList = Base::$oBase->Db_getList ( $sSql );
		$aList = $aList ['data'];
		foreach ( $aList as $iKey => $aRow ) {
			foreach ( $aRow as $sKey => $sValue ) {
				unset ( $aList [$iKey] [$sKey] );
				$sKey = preg_replace ( '/^act_/', '', $sKey );
				$aList [$iKey] [$sKey] = $sValue;
			}
			$aList [$iKey] ['status_value'] = $this->_aStatus [$aList [$iKey] ['status']];
		}
		return $aList;
	}
	
	public function scan() {
		$aFile = Base::$oBase->File_ls ( ROOT . '/' . Base::getConf ( 'base.action_path' ), 'file' );
		$aModel = array ();
		foreach ( $aFile as $sRow ) {
			$sName = ucfirst ( basename ( $sRow, 'action.class.php' ) );
			if ($sName == ucfirst ( basename ( $sRow ) )) {
				continue;
			}
			if (Base::$oBase->Db_getOne ( 'SELECT * FROM actions WHERE act_name = ?', $sName ) == false) {
				Base::$oBase->Db_exec ( 'INSERT INTO actions SET act_name = ?', $sName );
			}
			$this->_updateMethod ( $sName );
			$aModel [] = $sName;
		}
		$rRes = Base::$oBase->Db_query ( 'SELECT act_name name FROM actions' );
		while ( ($aRow = Base::$oBase->Db_getRow ( '$rRes' )) !== false ) {
			if (! in_array ( $aRow ['name'], $aModel )) {
				Base::$oBase->Db_exec ( 'DELETE FROM actions WHERE act_name = ?', $aRow ['name'] );
			}
		}
		Base::$oBase->Cache_del ( 'action' );
	}
	
	public function get($sName) {
		$sSql = 'SELECT * FROM actions WHERE act_name = ?';
		$aInfo = Base::$oBase->Db_getOne ( $sSql, $sName );
		if (is_array ( $aInfo )) {
			foreach ( $aInfo as $sKey => $sValue ) {
				unset ( $aInfo [$sKey] );
				$sKey = preg_replace ( '/^act_/', '', $sKey );
				$aInfo [$sKey] = $sValue;
			}
		}
		return $aInfo;
	}
	
	public function edit($aInfo) {
		if (array_key_exists ( $aInfo ['status'], $this->_aStatus )) {
			$aValue [] = $aInfo ['status'];
		} else {
			$aValue [] = 'close';
		}
		$aValue [] = $aInfo ['name'];
		Base::$oBase->Cache_del ( 'action' );
		Base::$oBase->Db_update ( 'actions', $aInfo, array ('name' => $aInfo ['name'] ) );
	}
	
	private function _updateMethod($sModelName) {
		$sText = file_get_contents ( ROOT . '/' . Base::getConf ( 'base.action_path' ) . '/' . strtolower ( $sModelName ) . 'action.class.php' );
		$aMatch = array ();
		preg_match_all ( '/public\s+function\s+(.+?)\(.*/i', $sText, $aMatch );
		$sSql = 'DELETE FROM action_methods WHERE acm_action_name = ?';
		Base::$oBase->Db_exec ( $sSql, $sModelName );
		$sSql = 'INSERT INTO action_methods VALUES (?, ?)';
		foreach ( $aMatch [1] as $sRow ) {
			Base::$oBase->Db_exec ( $sSql, array ($sModelName, trim ( $sRow ) ) );
		}
	}
	
	public function getMethodJson($bPrint = false) {
		$aData = $this->_getByActive ();
		if ($bPrint) {
			echo json_encode ( $aData );
		} else {
			return json_encode ( $aData );
		}
	}
	
	private function _getByActive() {
		$aData = Base::$oBase->Cache_get ( 'action' );
		if ($aData == '__NOCACHE__') {
			$aData = array ();
			$sSql = 'SELECT acm_action_name action_name, acm_method_name method_name
					FROM action_methods, actions
					WHERE act_status = ? AND act_name = acm_action_name
					ORDER BY action_name, method_name';
			Base::$oBase->Db_query ( $sSql, 'active' );
			while ( ($aRow = Base::$oBase->Db_getRow ()) !== false ) {
				$aData [$aRow ['action_name']] [] = $aRow ['method_name'];
			}
			Base::$oBase->Cache_set ( 'action', $aData, null, 0 );
		}
		return $aData;
	}
	
	public function checkActive($sName) {
		$aData = $this->_getByActive ();
		return array_key_exists ( $sName, $aData );
	}
}
?>
